﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;

namespace XQ.Framework.io.image
{
    /// <summary>
    ///用于在页面上显示缩略图
    ///传入response对象，原始图片绝对路径，宽，高。
    /// </summary>
    public class DrawSmallImg
    {
        private HttpResponse response = null;
        private string imgPath = null;
        private int height = 150;
        private int width = 150;
        private Bitmap bm = null;
        private Graphics g = null;
        private MemoryStream ms = null;
        /// <summary>
        /// 将图片转化为小图标显示到页面
        /// </summary>
        /// <param name="res">response对象</param>
        /// <param name="imgpath">需要显示图像的绝对路径</param>
        /// <param name="Width">显示宽度</param>
        /// <param name="Height">显示高度</param>
        public DrawSmallImg(HttpResponse res, string imgpath, int Width, int Height)
        {
            this.response = res;
            this.imgPath = imgpath;
            this.width = Width;
            this.height = Height;
        }
        /// <summary>
        /// 开始画图
        /// </summary>
        public void drawSmallImg()
        {
            if (!File.Exists(imgPath))
            {
                return;
            }
            response.ContentType = "image/Png";
            try
            {
                Image img = Image.FromFile(imgPath);
                int h = img.Height;
                int w = img.Width;
                width = (width < w) ? width : w;
                height = (height < h) ? height : h;
                bm = new Bitmap(width, height);
                g = Graphics.FromImage(bm);
                //设置高质量插值法  
                g.InterpolationMode = InterpolationMode.HighQualityBilinear;
                //设置高质量,低速度呈现平滑程度  
                g.SmoothingMode = SmoothingMode.HighQuality;
                //清空画布并设置背景色  
                g.Clear(Color.White);
                g.DrawImage(img, new Rectangle(0, 0, width, height), new Rectangle(0, 0, w, h), GraphicsUnit.Pixel);
                ms = new MemoryStream();
                string ext = new FileInfo(imgPath).Extension.ToLower();
                switch (ext)
                {
                    case ".png": bm.Save(ms, ImageFormat.Png); break;
                    case ".jpg": bm.Save(ms, ImageFormat.Jpeg); break;
                    case ".gif": bm.Save(ms, ImageFormat.Gif); break;
                    case ".bmp": bm.Save(ms, ImageFormat.Bmp); break;
                    default: bm.Save(ms, ImageFormat.Png); break;
                }
                //输出到HTTP流
                response.BinaryWrite(ms.ToArray());
            }
            catch (Exception err)
            {
                throw new Exception(err.Message);
            }
            finally
            {
                if (ms != null)
                { ms.Dispose(); }
                if (bm != null)
                { bm.Dispose(); }
                if (g != null)
                { g.Dispose(); }
            }
        }

        #region Tips
        //用g.drawImage()效果比直接改变bm大小bm=new BitMap(img,width,height); 显示效果好
        #endregion

    }
}